<?php
/**
 * Created by PhpStorm.
 * User: Jacob Shi
 * Date: 2016/6/20
 * Time: 12:06
 */

class Silk_Mapi_Model_Api2_Support_Rest extends Silk_Mapi_Model_Api2_Base
{
    protected function _http() {
        if (!$this->_http_client) {
            require_once "Zend/Http/Client.php";
            $this->_http_client = new Zend_Http_Client;
        }
        return $this->_http_client;
    }

    public function _retrieve()
    {
        $customer = $this->getCustomer();

        $infoUrl = Mage::getStoreConfig('general/api/customer_push_url');
        $infoUrl = trim($infoUrl,'/').'/supports?id='.$customer->getId();
        $result = array(
            'salesrep'=>'',
            'phone'=>''
        );

        $response = $this->_http()
            ->setUri($infoUrl)
            ->setHeaders("Content-Type", "application/json")
            ->request(Zend_Http_Client::GET);
        if(!$response) return $result;
        $html = $response->getBody();
        $json = Zend_Json::decode($html);
        $data = array();
        if($json['code']==200)
        {
            $data = $json['data'];
        }
        if(isset($data['name']))
        {
            $result['salesrep'] = $data['name'];
        }

        if(isset($data['mobile']))
        {
            $result['phone'] = $data['mobile'];
        }

        return $result;

    }
}